package org.jeecg.common.job.data.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.cache.DataCache;
import org.jeecg.common.constant.enums.FlagEnum;
import org.jeecg.common.datasource.jdbc.vo.PageInfo;
import org.jeecg.common.datasource.jdbc.vo.PageSort;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.job.JobManageCenter;
import org.jeecg.common.job.data.dao.JobInfoDao;
import org.jeecg.common.job.data.dao.JobLogDao;
import org.jeecg.common.job.data.service.IJobService;
import org.jeecg.common.job.data.vo.JobInfoVo;
import org.jeecg.common.job.data.vo.JobLogVo;
import org.jeecg.common.job.enums.JobConstants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zhaoguoqiang
 * @version 2020年01月19 15:59
 */
@Service
public class JobServiceImpl implements IJobService {

    @Resource
    private JobInfoDao jobInfoDao;

    @Resource
    private JobLogDao jobLogDao;

    @Override
    public JobInfoVo load(String id) {
        return jobInfoDao.load(id);
    }

    @Override
    public boolean save(JobInfoVo job) {
        // 判断定时任务是否存在
        if (this.jobInfoDao.findJobInfoVo(job.getBeanName(), job.getBeanMethod()) != null) {
            throw new JeecgBootException("该定时任务已经添加,请重新添加!");
        }
        job.setDelSign(FlagEnum.NO.getId());
        job.setCreateTime(new Date());
        job.setUpdateTime(new Date());
        jobInfoDao.save(job);
        this.cacheSelectJob();
        return true;
    }

    @Override
    public void update(JobInfoVo job, String... fieldName) {
        jobInfoDao.update(job, fieldName);
        this.cacheSelectJob();
    }

    @Override
    public boolean delete(String id) {
        jobInfoDao.delete(id);
        this.cacheSelectJob();
        return false;
    }

    @Override
    public boolean switchJob(String id, Integer type) {
        JobInfoVo job = jobInfoDao.load(id);
        job.setUpdateTime(new Date());
        if (type.equals(FlagEnum.NO.getId())) {
            job.setStatus(FlagEnum.NO.getId());
            jobInfoDao.update(job, "status", "updateTime");
        } else {
            job.setStatus(FlagEnum.YES.getId());
            jobInfoDao.update(job, "status", "updateTime");
        }
        this.cacheSelectJob();
        return true;
    }

    @Override
    public void listenerJob() {
        List<JobInfoVo> list = jobInfoDao.getJobList();
        for (JobInfoVo job : list) {
            if(job.getCreateTime() == null){
                job.setCreateTime(new Date());
                job.setUpdateTime(new Date());
                jobInfoDao.update(job, "createTime", "updateTime");
            }
            JobManageCenter.addJob(job.getId(), job.getCycleTime(), job.getBeanName(), job.getBeanMethod(), job.getExecuteMode(), job.getNodeName(), job.getUpdateTime());
        }

        DataCache.getSingleton().setString(JobConstants.CACHE_JOB_DATA_LIST, JSONObject.toJSONString(list));
    }

    @Override
    public void saveJobLog(JobLogVo jobLog) {
        this.jobLogDao.save(jobLog);
    }

    @Override
    public void deleteLogs(Date deleteDate) {
        this.jobLogDao.delete(deleteDate);
    }

    @Override
    public PageInfo<JobLogVo> getJobLogs(PageSort pageSort, String content) {
        return this.jobLogDao.getJobLogs(pageSort, content);
    }

    @Override
    public JobInfoVo findJobInfoVo(String beanName, String beanMethod) {
        return this.jobInfoDao.findJobInfoVo(beanName, beanMethod);
    }

    @Override
    public PageInfo<JobInfoVo> getJobPage(PageSort pageSort, String name) {
        PageInfo<JobInfoVo> page = jobInfoDao.getJobPage(pageSort, name);
        return page;
    }

    @Override
    public List<JobInfoVo> getCacheSelectJobList() {
        String jobListStr = DataCache.getSingleton().getString(JobConstants.CACHE_JOB_DATA_LIST);
        return JSONObject.parseArray(jobListStr, JobInfoVo.class);
    }

    @Override
    public void cacheSelectJob(){
        List<JobInfoVo> list = jobInfoDao.getJobList();
        DataCache.getSingleton().setString(JobConstants.CACHE_JOB_DATA_LIST, JSONObject.toJSONString(list));
    }

    @Override
    public JSONArray getJobConfig() {
        String listValue = DataCache.getSingleton().getString(JobConstants.CACHE_NODE_NAMES);
        return JSONArray.parseArray(listValue);
    }
}
